import { createSlice,createAsyncThunk } from "@reduxjs/toolkit";
import type { PayloadAction } from "@reduxjs/toolkit";


//异步操作
export const fetchUserById = createAsyncThunk(
    'user/fetchByIdStatus',
    async (userId: number ) => {
      const response = await new Promise((resolve, reject) => {
        resolve('success')
      })
      return response
    },
  )

const userSlice = createSlice({
  name: "user",
  initialState: {
    name: "John Doe",
    age: 30,
    email: "johndoe@example.com",
  },
  reducers: {
    updateName: (state, action:PayloadAction<string>) => {
      state.name = action.payload;
    },
    updateAge: (state, action:PayloadAction<number>) => {
      state.age = action.payload;
    },
    updateEmail: (state, action:PayloadAction<string>) => {
      state.email = action.payload;
    },
  },
  extraReducers: (builder) => {
    builder.addCase(fetchUserById.pending, (state,action) => {
        console.log('fetchUserById.pending',action,state)
    })
    builder.addCase(fetchUserById.fulfilled, (state,action) => {
        console.log('fetchUserById.fulfilled',action,state)
    })
  }
});
console.log(userSlice)
export const { updateName, updateAge, updateEmail } = userSlice.actions;

export default userSlice.reducer;